package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Operation;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.mapper.OperationMapper;
import com.example.demo.service.OperationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-10-10
*/
@Slf4j
@Service
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements OperationService {

    @Override
    public Page<Operation> listOperationsByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询operation: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<Operation> queryWrapper =  new QueryWrapper<Operation>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<Operation> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询operation完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public List<Operation> getAllOperations() {
        log.info("正在查询所有operation的数据");
        List<Operation> operations = super.list();
        log.info("查询所有的operation{}",(operations.isEmpty()?"无结果":"成功"));
        return operations;
    }

    @Override
    public List<Operation> getOperationsByIds(List<Integer> ids) {
        if (!ids.isEmpty()){
            log.info("正在查询operation中id为{}的数据",ids);
            List<Operation> operations = super.listByIds(ids);
            log.info("查询id为{}的operation{}",ids,(null == operations?"无结果":"成功"));
            return operations;
        }else{
            return null;
        }
    }

    @Override
    public Operation getOperationById(int id) {
        log.info("正在查询operation中id为{}的数据",id);
        Operation operation = super.getById(id);
        log.info("查询id为{}的operation{}",id,(null == operation?"无结果":"成功"));
        return operation;
    }

    @Override
    public int insertOperation(Operation operation) {
        log.info("正在插入operation");
        if (super.save(operation)) {
            log.info("插入operation成功,id为{}",operation.getOperationId());
            return operation.getOperationId();
        } else {
            log.error("插入operation失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteOperationById(int id) {
        log.info("正在删除id为{}的operation",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的operation成功",id);
            return id;
        } else {
            log.error("删除id为{}的operation失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateOperation(Operation operation) {
        log.info("正在更新id为{}的operation",operation.getOperationId());
        if (super.updateById(operation)) {
            log.info("更新d为{}的operation成功",operation.getOperationId());
            return operation.getOperationId();
        } else {
            log.error("更新id为{}的operation失败",operation.getOperationId());
            throw new BizException("更新失败[id=" + operation.getOperationId() + "]");
        }
    }

}
